package _01_array;

import java.util.Iterator;
import java.util.function.Consumer;
import java.util.stream.IntStream;

/**
 * @author: mornd
 * @dateTime: 2023/6/1 - 12:42
 * 自定义存储 int 类型的动态数据
 */
public class DynamicArray implements Iterable<Integer> {
    // 实际存储元素个数
    private int size;

    // 数组容量
    private int capacity;
    private int[] data;

    // 默认容量
    private static final int DEFAULT_CAPACITY = 8;

    // 默认的空数组
    private static final int[] DEFAULT_EMPTY_DATA = {};

    public DynamicArray() {
        this(DEFAULT_CAPACITY);
    }

    public DynamicArray(int capacity) {
        this.capacity = capacity;
        data = DEFAULT_EMPTY_DATA;
    }


    /**
     * 检查并扩容
     */
    private void checkAndGrow() {
        if (data == DEFAULT_EMPTY_DATA) {
            data = new int[capacity];
        }
        if (size == capacity) {
            int[] newArr = new int[capacity += capacity >>> 1];
            System.arraycopy(data, 0, newArr, 0, size);
            data = newArr;
        }
    }

    /**
     * 尾部插入
     *
     * @param e
     */
    public void addLast(int e) {
        add(size, e);
    }

    public void add(int index, int e) {
        checkAndGrow();
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("index Illegal");
        }
        System.arraycopy(data, index, data, index + 1, size - index);
        data[index] = e;
        size++;
    }

    public int get(int index) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("index Illegal");
        }
        return data[index];
    }

    /**
     * 删除尾部元素
     *
     * @return
     */
    private int removeLast() {
        int old = data[size - 1];
        data[--size] = 0; // = null
        return old;
    }

    /**
     * 按照给定下标删除元素，后排元素前移
     *
     * @param index
     */
    public int remove(int index) {
        if (index == size - 1) {
            return removeLast();
        } else {
            int old = data[index];
            System.arraycopy(data, index + 1, data, index, size - index - 1);
            size--;
            return old;
        }
    }

    /**
     * 遍历 Iterable 接口已经实现
     *
     * @param c
     */
    public void foreach(Consumer<Integer> c) {
        for (Integer i : this) {
            c.accept(i);
        }
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("[");
        for (int i = 0; i < size; i++) {
            sb.append(data[i]);
            if (i < size - 1) {
                sb.append(",");
            }
        }
        return sb.append("]").toString();
    }

    /**
     * 返回迭代器对象
     *
     * @return
     */
    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            int i = 0;

            @Override
            public boolean hasNext() {
                return i < size;
            }

            @Override
            public Integer next() {
                return data[i++];
            }
        };
    }

    public IntStream stream() {
        int[] newArr = new int[size];
        System.arraycopy(data, 0, newArr, 0, size);
        return IntStream.of(newArr);
    }
}
